﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using HTComics.Web.Common;
using DB = HTComics.Data;
using System.Data;

namespace HTComics.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            PriceBandCollection priceBandColl = new PriceBandCollection();
            PriceGuideCollection priceGuideColl = new PriceGuideCollection();

            DataTable dtPriceBands = DB.Misc.GetPriceBands();
            for (int i = 0; i <= dtPriceBands.Rows.Count - 1; i++)
            {
                priceBandColl.Add(new PriceBand()
                {
                    BandId = Convert.ToInt32(dtPriceBands.Rows[i]["BandId"]),
                    RangeStart = Convert.ToDouble(dtPriceBands.Rows[i]["RangeStart"]),
                    RangeEnd = Convert.ToDouble(dtPriceBands.Rows[i]["RangeEnd"])
                });
            }
            
            DataTable dtPriceGuide = DB.Misc.GetPriceGuide();
            for (int i = 0; i <= dtPriceGuide.Rows.Count - 1; i++)
            {
                PriceGuide guide = new PriceGuide();
                guide.ConditionId = Convert.ToInt32(dtPriceGuide.Rows[i]["ConditionId_1"]);
                guide.BandId = Convert.ToInt32(dtPriceGuide.Rows[i]["BandId"]);

                if (dtPriceGuide.Rows[i]["ConditionId_2"] != DBNull.Value)
                    guide.ConditionId_2 = Convert.ToInt32(dtPriceGuide.Rows[i]["ConditionId_2"]);

                if (dtPriceGuide.Rows[i]["Factor"] != DBNull.Value)
                    guide.Factor = Convert.ToDouble(dtPriceGuide.Rows[i]["Factor"]);

                if (dtPriceGuide.Rows[i]["Var_A"] != DBNull.Value)
                    guide.VarA = Convert.ToDouble(dtPriceGuide.Rows[i]["Var_A"]);

                if (dtPriceGuide.Rows[i]["Var_B"] != DBNull.Value)
                    guide.VarB = Convert.ToDouble(dtPriceGuide.Rows[i]["Var_B"]);

                priceGuideColl.Add(guide);
            }

            Application["PriceBands"] = priceBandColl;
            Application["PriceGuides"] = priceGuideColl;

            DataTable dtForexRate = DB.Misc.GetForexRate();
            Application["ForexRate"] = dtForexRate.Rows[0]["Rate"];
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Response.Redirect("~/Error/ErrorPage.aspx");
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}